import 'package:flutter/material.dart';
import 'package:flutter_application_1/components/choose_bottom_sheet/choose_bottom_sheet.dart';
import 'package:flutter_application_1/plugins/custom_sort_path.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

// 选择的方法
enum ChooseMethods {
  Camera,
  Assets,
}

class ChooseAssets {
  /// 显示选择方法弹框
  static Future<ChooseMethods?> showChooseMethodSheet(BuildContext context) {
    return showChooseBottomSheet<ChooseMethods?>(
      context: context,
      sheets: [
        ChooseButton(
          text: '相机拍摄',
          onTap: () {
            Navigator.pop(context, ChooseMethods.Camera);
          },
        ),
        ChooseButton(
          text: '选择文件',
          onTap: () {
            Navigator.pop(context, ChooseMethods.Assets);
          },
        ),
      ],
    );
  }

  /// 选择方法
  static Future<List<AssetEntity>> choose({
    required BuildContext context,

    /// 最多可选择的数量
    int maxAssets = 9,

    /// 选择器是否可以录像
    bool enableRecording = false,

    /// 选择器是否仅可以录像。只在 enableRecording 为 true 时有效。
    bool onlyEnableRecording = false,
  }) async {
    late List<AssetEntity> entitys = [];

    ChooseMethods? chooseMethods = await showChooseMethodSheet(context);

    if (chooseMethods == ChooseMethods.Camera) {
      AssetEntity? _entity = await camera(
        context: context,
        enableRecording: enableRecording,
        onlyEnableRecording: onlyEnableRecording,
      );
      if (_entity != null) {
        entitys.add(_entity);
      }
    } else if (chooseMethods == ChooseMethods.Assets) {
      final List<AssetEntity>? _entitys = await asset(
        context: context,
        maxAssets: maxAssets,
      );
      if (_entitys != null) {
        entitys.addAll(_entitys);
      }
    }

    return entitys;
  }

  static Future<AssetEntity?> camera({
    required BuildContext context,
    required bool enableRecording,
    required bool onlyEnableRecording,
  }) {
    return CameraPicker.pickFromCamera(
      context,
      enableRecording: enableRecording,
      onlyEnableRecording: onlyEnableRecording,
    );
  }

  static Future<List<AssetEntity>?> asset({
    required BuildContext context,
    required int maxAssets,
  }) {
    return AssetPicker.pickAssets(
      context,
      maxAssets: maxAssets,
      sortPathDelegate: CustomSortPathDelegate(),
    );
  }
}
